﻿#region Declaration

//------------------------------------------------------------------------------
// Compas Information Techonoly Co., Ltd
// The Initial Developer of the Original Code is CompasSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

#endregion

#region Using Directives

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;
using System.Web;

#endregion

namespace QuickDev.Common.Security
{
    public class Security
    {
        public static string CreateSalt( int size )
        {
            // Generate a cryptographic random number using the cryptographic service provider
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[size];
            rng.GetBytes( buff );
            // Return a Base64 string representation of the random number
            return Convert.ToBase64String( buff );
        }

        public static string CreatePasswordHash( string pwd , string salt )
        {
            string saltAndPwd = String.Concat( pwd , salt );
            string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile( saltAndPwd , "SHA1" );
            return hashedPwd;
        }

        public static string EncryptQueryString( string sQuery )
        {
            return HttpUtility.UrlEncode( Encrypts.Encrypt( sQuery ) );
        }

        public static string DecryptQueryString( string sQuery )
        {
            return Encrypts.Decrypt( HttpUtility.UrlDecode( sQuery ) );
        }
    }
}
